package org.android.pasos.activities.firstview;

import org.android.pasos.Constants;
import org.android.pasos.DatosTrama;
import org.android.pasos.R;
import org.android.pasos.data.PasosApplication;
import org.android.pasos.util.UtilJSON;
import org.android.pasos.util.UtilTrama;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;

public class AssistanceActivity extends Activity {
	private Intent intent = null;
	private SharedPreferences mPrefs;
	private String si;
	private String no;

	/** Called when the activity is first created. */

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.assistance);
		
		si = this.getString(R.string.si);
		no = this.getString(R.string.no);

		mPrefs = getSharedPreferences("PasosPreferences", MODE_PRIVATE);

		PasosApplication appState = ((PasosApplication) getApplicationContext());
		String grupo = appState.getGrupo();
		if (grupo.equals(Constants.ANCIANO)) {
			new AlertDialog.Builder(this)
					// ancianos
					.setMessage(R.string.Qanciano1)
					.setPositiveButton(si, mQuestion1YesListener)
					.setNegativeButton(no, mQuestion1NoListener).show();
		} else if (grupo.equals(Constants.DISCAPACITADO)) {
			new AlertDialog.Builder(this)
					// discapacitados
					.setMessage(R.string.Qanciano1)
					.setPositiveButton(si, mQuestion1YesListener)
					.setNegativeButton(no, mQuestionDiscapacitado1NoListener)
					.show();
		} else if (grupo.equals(Constants.MALTRATADO)) {
			new AlertDialog.Builder(this)
					// maltratada
					.setMessage(R.string.Qmaltratada1)
					.setPositiveButton(si, mQuestion2Maltratada)
					.setNegativeButton(no, mQuestion1YesListener).show();
		} else if (grupo.equals(Constants.MONTANERO)) {
			new AlertDialog.Builder(this)
					// montañero
					.setMessage(R.string.Qmaltratada1)
					.setPositiveButton(si, mQuestion2Montanero)
					.setNegativeButton(no, mQuestion1YesListener).show();
		}
	}

	/* Dialogos dirigidos a personas de avanzada edad */
	private void mostrarQ2() {
		new AlertDialog.Builder(this).setMessage(R.string.Qanciano2)
				.setPositiveButton(si, mQuestion2YesListener)
				.setNegativeButton(no, mQuestion2NoListener).show();
	}

	private void mostrarQ3a() {
		new AlertDialog.Builder(this).setMessage(R.string.Qanciano3a)
				.setPositiveButton(si, mQuestion3aYesListener)
				.setNegativeButton(no, mDespedidaListener).show();
	}

	private void mostrarQ3b() {
		new AlertDialog.Builder(this).setMessage(R.string.Qanciano3b)
				.setPositiveButton(si, mQuestion3bYesListener)
				.setNegativeButton(no, mDespedidaListener).show();
	}

	private void mostrarQ4a() {
		new AlertDialog.Builder(this).setMessage(R.string.Qanciano4a)
				.setPositiveButton(si, mQuestion4aYesListener).show();
	}

	private void mostrarQ4b() {
		new AlertDialog.Builder(this).setMessage(R.string.Qanciano4b)
				.setPositiveButton(si, mQuestion4bYesListener).show();
	}

	private void mensajeDespedida() {
		new AlertDialog.Builder(this).setMessage(R.string.Qdespedida)
				.setPositiveButton(si, mDespedidaListener).show();
	}

	/* Dialogos dirigidos a personas discapacitadas */
	private void mostrarQ2Discapacitados() {
		new AlertDialog.Builder(this).setMessage(R.string.Qdiscapacitado2)
				.setPositiveButton(si, mQuestion2aDiscapacitados)
				.setNegativeButton(no, mQuestion2bDiscapacitados).show();
	}

	private void mostrarRespuestaQ2aDiscapacitados() {
		new AlertDialog.Builder(this).setMessage(R.string.Qdiscapacitado2a)
				.setPositiveButton(si, mDespedidaAsistenciaListener).show();
	}

	private void mostrarQ3Discapacitados() {
		new AlertDialog.Builder(this).setMessage(R.string.Qanciano3b)
				.setPositiveButton(si, mQuestion3bYesListener)
				.setNegativeButton(no, mQuestion3bDiscapacitados).show();
	}

	private void mostrarRespuestaQ3bDiscapacitados() {
		new AlertDialog.Builder(this).setMessage(R.string.Qdiscapacitado2a)
				.setPositiveButton(si, mDespedidaAsistenciaListener).show();
	}

	private void mostrarQ2Maltratada() {
		new AlertDialog.Builder(this).setMessage(R.string.Qmaltratada2)
				.setPositiveButton(si, mQuestion3Maltratada)
				.setNegativeButton(no, mQuestion2bDiscapacitados).show();
	}

	private void mostrarQ3Maltratada() {
		new AlertDialog.Builder(this).setMessage(R.string.Qmaltratada3)
				.setPositiveButton(si, mQuestion3aMaltratada)
				.setNegativeButton(no, mQuestion3bDiscapacitados).show();
	}

	private void mostrarRespuestaQ3aMaltratada() {
		new AlertDialog.Builder(this).setMessage(R.string.Qmaltratada3a)
				.setPositiveButton(si, mDespedidaAsistenciaListener).show();
	}

	private void mostrarQ2Montanero() {
		new AlertDialog.Builder(this).setMessage(R.string.Qanciano3a)
				.setPositiveButton(si, mQuestion3Montanero)
				.setNegativeButton(no, mQuestion4Montanero).show();
	}

	private void mostrarQ3Montanero() {
		new AlertDialog.Builder(this).setMessage(R.string.Qmontanero3)
				.setPositiveButton(si, mQuestion3aMaltratada)
				.setNegativeButton(no, mQuestion2aDiscapacitados).show();
	}

	private void mostrarQ4Montanero() {
		new AlertDialog.Builder(this).setMessage(R.string.Qmontanero4)
				.setPositiveButton(si, mQuestion4aMontanero)
				.setNegativeButton(no, mQuestion3bDiscapacitados).show();
	}

	private void mostrarQ4aMontanero() {
		new AlertDialog.Builder(this).setMessage(R.string.Qmontanero4a)
				.setPositiveButton(si, mDespedidaAsistenciaListener).show();
	}

	OnClickListener mQuestion1YesListener = new OnClickListener() {

		public void onClick(DialogInterface dialog, int which) {
			mensajeDespedida();
		}
	};

	OnClickListener mQuestion1NoListener = new OnClickListener() {

		public void onClick(DialogInterface dialog, int which) {
			mostrarQ2();
		}
	};

	OnClickListener mQuestion2YesListener = new OnClickListener() {

		public void onClick(DialogInterface dialog, int which) {
			mostrarQ3a();
		}
	};

	OnClickListener mQuestion2NoListener = new OnClickListener() {

		public void onClick(DialogInterface dialog, int which) {
			mostrarQ3b();
		}
	};

	OnClickListener mQuestion3aYesListener = new OnClickListener() {

		public void onClick(DialogInterface dialog, int which) {
			mostrarQ4a();
		}
	};

	OnClickListener mQuestion3bYesListener = new OnClickListener() {

		public void onClick(DialogInterface dialog, int which) {
			mostrarQ4b();
		}
	};

	OnClickListener mQuestion4aYesListener = new OnClickListener() {

		public void onClick(DialogInterface dialog, int which) {
			volverPrincipal();
		}
	};

	OnClickListener mQuestion4bYesListener = new OnClickListener() {

		public void onClick(DialogInterface dialog, int which) {
			volverPrincipal();
		}
	};

	OnClickListener mDespedidaListener = new OnClickListener() {

		public void onClick(DialogInterface dialog, int which) {
			volverPrincipalSinAsistencia();
		}
	};

	OnClickListener mDespedidaAsistenciaListener = new OnClickListener() {

		public void onClick(DialogInterface dialog, int which) {
			volverPrincipal();
		}
	};

	OnClickListener mQuestionDiscapacitado1NoListener = new OnClickListener() {

		public void onClick(DialogInterface dialog, int which) {
			mostrarQ2Discapacitados();
		}
	};

	OnClickListener mQuestion2aDiscapacitados = new OnClickListener() {

		public void onClick(DialogInterface dialog, int which) {
			mostrarRespuestaQ2aDiscapacitados();
		}
	};

	OnClickListener mQuestion2bDiscapacitados = new OnClickListener() {

		public void onClick(DialogInterface dialog, int which) {
			mostrarQ3Discapacitados();
		}
	};

	OnClickListener mQuestion3bDiscapacitados = new OnClickListener() {

		public void onClick(DialogInterface dialog, int which) {
			mostrarRespuestaQ3bDiscapacitados();
		}
	};

	OnClickListener mQuestion2Maltratada = new OnClickListener() {

		public void onClick(DialogInterface dialog, int which) {
			mostrarQ2Maltratada();
		}
	};

	OnClickListener mQuestion3Maltratada = new OnClickListener() {

		public void onClick(DialogInterface dialog, int which) {
			mostrarQ3Maltratada();
		}
	};

	OnClickListener mQuestion3aMaltratada = new OnClickListener() {

		public void onClick(DialogInterface dialog, int which) {
			mostrarRespuestaQ3aMaltratada();
		}
	};

	OnClickListener mQuestion2Montanero = new OnClickListener() {

		public void onClick(DialogInterface dialog, int which) {
			mostrarQ2Montanero();
		}
	};

	OnClickListener mQuestion3Montanero = new OnClickListener() {

		public void onClick(DialogInterface dialog, int which) {
			mostrarQ3Montanero();
		}
	};

	OnClickListener mQuestion4Montanero = new OnClickListener() {

		public void onClick(DialogInterface dialog, int which) {
			mostrarQ4Montanero();
		}
	};

	OnClickListener mQuestion4aMontanero = new OnClickListener() {

		public void onClick(DialogInterface dialog, int which) {
			mostrarQ4aMontanero();
		}
	};

	/************************************************************/

	private void volverPrincipal() {
		DatosTrama dt = UtilTrama.creartrama(getApplicationContext());
		UtilJSON.enviarIncidencia(getApplicationContext(),
				Constants.URL_INCIDENCIAS_REST, Constants.ASISTENCIA, dt);
		Log.d(Constants.LOGTAG, "Asistencia ya!");
		Intent intent = new Intent(Constants.INTENT_MENU_PRINCIPAL);
		startActivity(intent);
	}

	private void volverPrincipalSinAsistencia() {
		Intent intent = new Intent(Constants.INTENT_MENU_PRINCIPAL);
		startActivity(intent);
	}
}
